package com.zhou.jiancai.ui;

import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/3/10.
 */


    public class ChildViewPager extends ViewPager {
        /** 触摸时按下的点 **/
        PointF downPoint = new PointF();
        /** 触摸时当前的点 **/
        PointF curP = new PointF();
        OnSingleTouchListener onSingleTouchListener;


        public ChildViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }

        public ChildViewPager(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent arg0) {
            // TODO Auto-generated method stub
            //当拦截触摸事件到达此位置的时候，返回true，
            //说明将onTouch拦截在此控件，进而执行此控件的onTouchEvent
            return true;
        }

    @Override
    public boolean onTouchEvent(MotionEvent evt) {
        switch (evt.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 记录按下时候的坐标
                downPoint.x = evt.getX();
                downPoint.y = evt.getY();
                if (this.getChildCount() > 1) { //有内容，多于1个时
                    // 通知其父控件，现在进行的是本控件的操作，不允许拦截
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (this.getChildCount() > 1) { //有内容，多于1个时
                    // 通知其父控件，现在进行的是本控件的操作，不允许拦截
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_UP:
                // 在up时判断是否按下和松手的坐标为一个点
                if (PointF.length(evt.getX() - downPoint.x, evt.getY()
                        - downPoint.y) < (float) 5.0) {
                    onSingleTouch();
                    return true;
                }
                break;
        }
        return super.onTouchEvent(evt);
    }
    /**
         * 单击
         */
        public void onSingleTouch() {
            if (onSingleTouchListener!= null) {

                onSingleTouchListener.onSingleTouch();
            }
        }

        /**
         * 创建点击事件接口
         * @author wanpg
         *
         */
        public interface OnSingleTouchListener {
            public void onSingleTouch();
        }

        public void setOnSingleTouchListener(OnSingleTouchListener onSingleTouchListener) {
            this.onSingleTouchListener = onSingleTouchListener;
        }






    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

      getParent(). requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }




    private ViewGroup parent;

    public void setParent(ViewGroup parent) {
        this.parent = parent;
    }




}





